<template>
  <!-- 添加或修改维修对话框 -->
  <el-dialog :title="title"
             :visible.sync="addVisible"
             width="500px"
             append-to-body
             :before-close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="报修人" prop="reportName">
        <el-input v-model="form.reportName" placeholder="请输入报修人"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话"/>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入地址"/>
      </el-form-item>
      <el-form-item label="报修时间" prop="createTime">
        <el-date-picker clearable
                        v-model="form.createTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择报修时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="详情" prop="remark">
        <el-input
          type="textarea"
          v-model="form.remark"
          :rows="4"
          placeholder="请输入详情"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addReport, updateReport} from "@/api/system/report";

export default {
  name: "add",
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    form: {
      type: Object,
      required: true
    },
    getList: {
      type: Function,
      required: true
    },
    reset: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      addVisible: false,
      // 表单校验
      rules: {
        reportName: [
          {required: true, message: "报修人不能为空", trigger: "blur"}
        ],
        phone: [
          { required: true,
            pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
            message: '联系电话不能为空，并且输入至少11位号码', trigger: 'change'},
        ],
        address: [
          {required: true, message: "地址不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "详情不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "报修时间必须添加", trigger: "change"}
        ]
      }

    }
  },
  watch: {
    visible(n) {
      this.addVisible = n
    }
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reportId != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit("update:visible", false)
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit("update:visible", false)
              this.getList();
            });
          }
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:visible", false)
      this.reset();
    },
  }
}
</script>
